<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$return['ischecklogin']=cfc('core')->checklogin('','',true);
	$this->model('member')->upmember();
	set_html('title','会员中心');
	$member=model('core/member')->detail($_SESSION['uid']);
	$this->model('model')->checklogin();	
	$user=pdo_get('jy_aixiaoyi_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	//VIP时间：
	$user['vipstatus']=$user['viptime']>time()?1:2;
	$user['viptime']=date('Y/m/d',$user['viptime']);
	$member=array_merge($member,$user);
	$member['avatar']=tomedia($member['avatar']);
	$return['member']=$return['member2']=$member;
	$return['user']=$user;
	$return['showrecharge']=$_RQ['showrecharge']?:'';
	$return['item']=array();
	if($this->plugin_exists('jy_commission')){
		$return['jy_commission']=true;	
	}
	$return['basic']['bi_rate']=$return['basic']['bi_rate']?:1;
	$return['bi_rate']=round($return['basic']['bi_rate'],2)*1;
	//已经填写绑定
	$return['guwen']=pdo_get('jy_aixiaoyi_guwen',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	//获取当前时间：
	$showtypetime1=mktime(10,30,0,date('m'),date('d'),date('Y'));
	$showtypetime2=mktime(14,00,0,date('m'),date('d'),date('Y'));
	$showtypetime3=mktime(17,00,0,date('m'),date('d'),date('Y'));
	$showtypetime4=mktime(21,00,0,date('m'),date('d'),date('Y'));
	if(TIMESTAMP<$showtypetime1){
		$return['showtype']=1;
	}elseif(TIMESTAMP>$showtypetime1 && TIMESTAMP<$showtypetime2){
		$return['showtype']=2;
	}elseif(TIMESTAMP>$showtypetime2 && TIMESTAMP<$showtypetime3){
		$return['showtype']=3;
	}elseif(TIMESTAMP>$showtypetime3 && TIMESTAMP<$showtypetime4){
		$return['showtype']=4;
	}elseif(TIMESTAMP>$showtypetime4){
		$return['showtype']=5;
	}
	$return['showdaytypetitle']=$this->showdaytype[$return['showtype']];
	$homenav =array(
		array('title'=>'我的顾问','icon'=>'icon-kefuyouxian','action'=>'jy_aixiaoyi/myguwen/'),
		array('title'=>'某某智导产品顾问记录','icon'=>'icon-liaotian','action'=>'jy_aixiaoyi/mychat/'),
		array('title'=>'某某体智识记录','icon'=>'icon-data','action'=>'jy_aixiaoyi/tilog/'),
	);
	$return['pluginset']['homenav']=$return['pluginset']['homenav']?:$homenav;
	exi($return);
}
if($_RQ['op']=='getguwens'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$total='';
	$list=pdo_getslice('jy_aixiaoyi_guwen',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['sextitle']=$this->sex[$item['sex']];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list;
	$return['member']=pdo_get('jy_aixiaoyi_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$coremember=model('core/member')->detail($_SESSION['uid'],array('mobile','groupid','avatar','nickname','sex','fromuser'));
	$return['member']['nickname']=$return['member']['title']?:$coremember['nickname'];
	$return['member']['title']=$return['member']['title']?:$coremember['nickname'];
	$return['member']['mobile']=$return['member']['mobile']?:$coremember['mobile'];
	$return['member']['sex']=$return['member']['sex']?:$coremember['sex'];
	$return['member']['sextitle']=$this->sex[$return['member']['sex']];
	$return['member']['isguwen']=$return['member']['guwenid']?1:0;
	if(!$return['member']['avatar']){
		$return['member']['avatar']=$return['member']['sex']==1?'../addons/jy_aixiaoyi/resource/images/nan1.png':'../addons/jy_aixiaoyi/resource/images/nv1.png';
	}
	if(!$return['member']['guwenid']){
		$guwenid=$coremember['fromuser']?pdo_getcolumn('jy_aixiaoyi_guwen',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'mid'=>$coremember['fromuser']),'id'):0;
		$guwenid=$guwenid?:pdo_getcolumn('jy_aixiaoyi_guwen',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'isdefault'=>1),'id');
		$return['member']['guwenid']=$guwenid?:pdo_getcolumn('jy_aixiaoyi_guwen',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),'id');
	}
	exi($return);
}
if($_RQ['op']=='setinfo'){
	$post=get_postdata();
	if(!$post['title'] || !$post['mobile'] || !$post['sex']){
		exi('所有内容不能为空！');
	}
	if(!$post['guwenid']){
		exi('请选择顾问！');
	}
	$update=array(
		'mobile'=>$post['mobile'],
		'title'=>$post['title'],
		'sex'=>$post['sex'],
		'guwenid'=>$post['guwenid'],
	);
	pdo_update('jy_aixiaoyi_member',$update,array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$update=array(
		'mobile'=>$post['mobile'],
		'nickname'=>$post['title'],
		'sex'=>$post['sex'],
	);
	model('core/member')->member_update($_SESSION['uid'],$update);
	exi('操作成功！');
}
//购买会员
if($_RQ['op']=='buyvip'){
	set_html('title','购买会员');
	$basic=$this->plugin_setting('basic');
	$post=array();
	$post['paytype']=1;
	$post['num']=1;
	$post['checked']=true;
	$return['systemxieyi']=$this->plugin_setting('systemxieyi');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$packnum=$postdata['num'];
		$packname=$postdata['paytype'];
		if($packname==1){
			$fee=$basic['mfee']*$packnum;
		}elseif($packname==2){
			$fee=$basic['jfee']*$packnum;	
		}elseif($packname==3){
			$fee=$basic['yfee']*$packnum;
		}
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'packnum'=>$packnum,
			'ordersn'=>get_ordersn('jy_aixiaoyi_viporder','ordersn'),
			'fee'=>$fee,
			'packname'=>$packname,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_aixiaoyi_viporder',$insert);
		$id=pdo_insertid();
		$params['fee']=$fee;
		$params['title']='购买会员订单';
		$params['id']=$id;
		$params['plugin']='jy_aixiaoyi';
		//$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		//params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_aixiaoyi/home/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	$return['fee']=price($basic['mfee']);
	$return['fee0']=$basic['mfee0']?price($basic['mfee0']):price($basic['mfee']*1.5);
	$basic=$this->plugin_setting('basic');
	$return['vipbg']=$basic['vipbg'];
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_aixiaoyi_viporder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_aixiaoyi_viporder',array('status'=>1,'ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//计算到期时间
	$startTime =time();
	$user=pdo_get('jy_aixiaoyi_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	if($order['packname']==3){
		$order['packnum']=$order['packnum']*12;
	}elseif($order['packname']==2){
		$order['packnum']=$order['packnum']*3;
	}
	if($user['viptime']!=0){
		$addTime=$user['viptime']+$order['packnum']*30*24*3600;
	}else{
		$addTime =$startTime+$order['packnum']*30*24*3600;
	}
	pdo_update('jy_aixiaoyi_member',array('isvip'=>1,'viptime'=>$addTime),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	exi('支付成功','success','jy_aixiaoyi/home/main/');
}
//VIP购买记录
if($_RQ['op']=='viplog'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($adminuid!=$_SESSION['uid']){
		$params['mid']=$_SESSION['uid'];
	}
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_aixiaoyi_viporder',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			if($item['ispay']==0){
				$item['ispay']='待支付';
			}else{
				$item['ispay']='支付成功';
			}
			$item['nickname']=pdo_getcolumn('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['packtime']=$item['packtime']?date('Y-m-d H:i:s',$item['packtime']):'无效';
		}
	}
	$return['list']=$list;
	set_html('title','VIP购买记录');
	exi($return);
}
if($_RQ['op']=='recharge'){
	$post=get_postdata();
	if(intval($post['jifen'])<=0){
		exi('请先设置额度！并且为整数！','error');
	}
	$this->setting['bi_rate']=$this->setting['bi_rate']?:1;
	$bi_rate=round($this->setting['bi_rate'],2)*1;
	$fee=$post['jifen']/$bi_rate;
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'couponid'=>intval($post['couponid']),
		'fee'=>$fee,
		'credit'=>$post['jifen'],
		'type'=>$post['type'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'recharge',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'金币充值',
		'plugin'=>'core',
		//'pay_type'=>'wechat',
		'no_type'=>array('hdfk'),
		'back_url'=>'core/member.xunibi/pay_result',
		'success_url'=>'core/member.xunibi//type='.$post['type'],
		'error_url'=>'core/member.xunibi//type='.$post['type'],
	);
	if($_SESSION['container']=='aliapp' && $_SESSION['aliappopenid']){
		$params['pay_type']='alipay';
	}
	$pay_id=cfc('pay')->create_pay_order($params);
	exi($pay_id);
}